<template>
	<view>
		<u-navbar :border-bottom="false" title="销售设置" :title-size="36" :title-bold="true" :title-color="textColor" :background="{backgroudColor: bgColor}">
		</u-navbar>
		<view class="u-m-b-20">
			<u-cell-group :border="false">
				<u-cell-item title="收款账号" value="配置收款账号" @click="toPage('shoukuan')"></u-cell-item>
				<u-cell-item title="企业微信" value="配置企业微信收款号" @click="toPage('wechat')"></u-cell-item>
				<u-cell-item title="企业支付宝" :value="isAlipayValue"  @click="aliPaySignFn()"> 
				</u-cell-item>
			</u-cell-group>
		</view>
		
		<view class="u-m-b-20">
			<u-cell-group :border="false">
				<u-cell-item title="销售规则" @tap="toPage('saleRule')"></u-cell-item>
			</u-cell-group>
		</view>
		<view class="u-m-b-20">
			<u-cell-group :border="false">
				<u-cell-item title="电子发票" value="已开通" @tap="toPage('fapiao')"></u-cell-item>
			</u-cell-group>
		</view>
		<view class="u-m-b-20">
			<u-cell-group :border="false">
				<u-cell-item title="品牌商代授权" value="" @tap="toPage('ownerAuth')"></u-cell-item>
			</u-cell-group>
		</view>
		<!-- 二维码显示 -->
		<u-modal v-model="erweimaShow" width="60%" :show-title="false" :show-confirm-button="false" :show-cancel-button="false" :mask-close-able="true">
			<view class="slot-content">
				<img :src="erweima" style="width: 100%;" mode="" />
			</view>
			<view style="text-align: center;margin:0rpx 0 10rpx 0;">用支付宝扫描二维码进行签约</view>
			<!-- #ifdef APP-PLUS -->
			<u-button style="color: #FF551A;border-left:none;border-right: none;border: none;" @click="saveLocationFn">点击保存到相册</u-button>
			<!-- #endif -->
		</u-modal>
		<view class="u-m-b-20">
			<u-cell-group :border="false">
				<u-cell-item title="职位管理" value="设置采购、调酒师等职位" :border-bottom="false" @click="toPage('zhiwei')"></u-cell-item>
				<u-cell-item title="员工管理" value="管理店内员工" @click="toPage('yuangong')"></u-cell-item>
			</u-cell-group>
		</view>
		<u-modal v-model="delAlipay" title="解除签约" :content="delAlipayContent" @confirm="delAlipayFn()"></u-modal>
		<u-popup v-model="alipayShow" border-radius="10"  mode="center"  >
			<view class="alipay-pop">
				<view class="alipay-pop-text">
					<text >绑定支付宝</text>
				</view>
				<u-button class="alipay-pop-btn" type="primary" :loading="authLoading" @click="signPay('alipay')" v-if="!payList.isAuthorizedOfAlipay">点击授权</u-button>
				<u-button class="alipay-pop-btn" type="success" disabled="true" v-if="payList.isAuthorizedOfAlipay">已授权</u-button>
				<image src="/static/down.png" mode="" class="down-image"></image>
				<u-button class="alipay-pop-btn" type="primary" v-if="!payList.isValidOfAlipay" @click="certifiedOfAlipayFn()">检测是否开通当面付</u-button>
				<u-button class="alipay-pop-btn" type="success" disabled="true" v-if="payList.isValidOfAlipay">已签约</u-button>
				<image src="/static/down.png" mode="" class="down-image"></image>
				<u-button class="alipay-pop-btn"  type="primary" @click="complateAlipay()">完成绑定</u-button>
			</view>
		</u-popup>
		<u-modal v-model="alipayFtF" title="当面付签约状态" :content="alipayFtFtext" ></u-modal>
		<view v-html="fromDom"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shoukuanInfo: {},
				shoukuanFlag: false,
				shopId:null,
				erweimaShow:false,
				alipayFtF:false,
				alipayFtFtext:'',
				alipayFtFStatus:{
					unauthorized:'未授权',
					none:'未签约，表示还没有签约该产品',
					valid:'已生效，表示合约已经生效，不需要再签约了',
					restrictValid:'受限生效，表示合约已经生效，但是资料不全，功能受限',
					audit:'审核中，已经有合约在审核中，请等待审核完成',
					waitConfirm:'待商户确认协议，合约已经审核通过，需要商户确认后合约才生效',
					auditReject:'审核未通过',
					invalid:'合约失效，曾经签过合约，但已经失效了，可以重新发起签约',
					restrictInvalid:'受限失效，受限合约失效了'
				},
				
				erweima:'',
				payList:{},
				isAlipayValue:'',
				isAlipayId:null,
				delAlipay:false,
				delAlipayContent:'解除绑定后，客户未支付的订单将无法支付，请慎重决定！',
				alipayShow:false,
				authLoading:false,
				fromDom:''
			}
		},
		onLoad(options){
			console.log(options)
			this.shopId = options.id;
			// this.getPayList(options.id)
		},
		onShow() {
			console.log('authShow')
			this.getPayList(this.shopId)
		},
		methods: {
			// 跳转页面
			toPage(page) {
				if(page == 'zhiwei'||page == 'yuangong'||page == 'ownerAuth'||page == 'saleRule'){
					this.$mRouter.push({
						route: '/pages/sale/shezhi/' + page+'?shopId='+this.shopId
					})
				}else if(page == 'shoukuan'){
					if (this.shoukuanFlag) {
						page = 'shoukuan_edit?data='+JSON.stringify(this.shoukuanInfo)+'&id='+this.shopId;
					}
					this.$mRouter.push({
						route: '/pages/sale/shezhi/' + page
					})
				}
				else{
					uni.showToast({
						title: "本功能开在开发中~",
						icon: 'none'
					})
				}
			},
			// 微信支付宝绑定信息获取
			async getPayList(id){
				let params = {
					shopId:id
				}
				uni.showLoading({
					title:'获取支付配置...'
				})
				const res = await this.$setHttp.get('api/Shop/GetShopCollections', { header:{shopId:this.shopId},params:params });
				uni.hideLoading()
				console.log(res)
				if(res.code == 200){
					this.payList = res.data;
						
					if(this.payList.isAuthorizedOfAlipay&&this.payList.isValidOfAlipay){
						this.isAlipayValue = '解除签约';
					}else{
						this.isAlipayValue = '配置企业支付宝收款号';
					}
					if(this.payList.shopCollections.length!==0){
						let ary = res.data.shopCollections.find(item=>item.type === 'Alipay')
		
						if(ary){
							this.isAlipayId =ary.id;
						}else{
							this.isAlipayId = null;
						}
					}else{
						this.isAlipayValue = '配置企业支付宝收款号'
					}
					if(this.payList.shopCollections.length !== 0){
						this.shoukuanFlag = true;
						this.shoukuanInfo = this.payList.shopCollections[0];
					}
				}
			},
			// 点击保存图片到相册 （已废除）
			saveLocationFn(){
				let base64 = this.erweima;
				 const bitmap = new plus.nativeObj.Bitmap("test");
				     bitmap.loadBase64Data(base64, function() {
				         const url = "_doc/" + new Date().getTime() + ".png";  // url为时间戳命名方式
				         console.log('saveHeadImgFile', url)
				         bitmap.save(url, {
				             overwrite: true,  // 是否覆盖
				             // quality: 'quality'  // 图片清晰度
				         }, (i) => {
				             uni.saveImageToPhotosAlbum({
				                 filePath: url,
				                 success: function() {
				                     uni.showToast({
				                         title: '图片保存成功',
				                         icon: 'none'
				                     })
				                     bitmap.clear()
				                 }
				             });
				         }, (e) => {
				             uni.showToast({
				                 title: '图片保存失败',
				                 icon: 'none'
				             })
				             bitmap.clear()
				         });
				     }, (e) => {
				         uni.showToast({
				             title: '图片保存失败',
				             icon: 'none'
				         })
				         bitmap.clear()
				}); 
			},
			// 点击绑定支付宝
			aliPaySignFn(){
				if(this.isAlipayValue === '解除签约'){
					this.delAlipay = true;
				}else{
					// this.signPay('alipay')
					console.log(222)
					this.alipayShow = true;
				}
			},
			// 支付宝授权
			async signPay(type){
				this.authLoading = true;
				let params = { shopId: this.shopId,type:type }
				// const res = await this.$newHttp.get('api/Shop/GenerationSignQrCode', { params:params })
				// const res = await this.$newHttp.get('api/Shop/GenerationSignUrl', { params:params })
				// const res = await this.$newHttp.get('api/Shop/AlipayInvitationIsAuthorization', { params:params })
				const res = await this.$setHttp.post('api/ThirdParty/alipay/AlipayGetAuthUrl?shopId='+ this.shopId,{},{header:{shopId:this.shopId}} )
				
				console.log(res)
				if(res.code === 200){
					this.authLoading = false;
					// this.erweima = 'data:image/jpeg;base64,'+ res.data
					// this.erweimaShow = true;
					// #ifdef  H5
					location.href= res.data;
					// #endif
					// #ifdef APP-PLUS
					let device = uni.getSystemInfoSync().platform;
					if (device === 'ios') {
						plus.runtime.openURL('alipay://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode='+encodeURIComponent(res.data))
					} else {
						plus.runtime.openURL('alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode='+encodeURIComponent(res.data))
					}
					// #endif
				}else{ 
					this.authLoading = false;
					uni.showToast({
						title: res.erroMessage,
						icon: 'none'
					}) 
				}
			},
			// 支付宝签约状态获取
			async certifiedOfAlipayFn(){
					// // #ifdef  H5
					// location.href=
					// 'alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode='+encodeURIComponent('https://spcenter.alipay.com/workspace/invite-sign?appId=2021003116611913&sign=OTwT8mQoDsV/inVnjBU66UfXF5iGBfqOtH0RBbEHA6I=')
					// // #endif
					// // #ifdef APP-PLUS
					// plus.runtime.openURL('alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode='+encodeURIComponent('https://spcenter.alipay.com/workspace/invite-sign?appId=2021003116611913&sign=OTwT8mQoDsV/inVnjBU66UfXF5iGBfqOtH0RBbEHA6I='))
					// // #endif
					const res = await this.$newHttp.post('api/ThirdParty/alipay/AlipayQueryShopProducts?shopId='+ this.shopId);
					if(res.code === 200){
						this.payList.isValidOfAlipay = res.data.isValid;
						let goWebTip = res.data.status === 'valid'?'':',请联系客服线下签约当面付后重新绑定';
						this.alipayFtFtext = this.alipayFtFStatus[res.data.status]+goWebTip;
						this.alipayFtF = true;
					}else{
						this.$u.toast('获取当面付签约状态失败');
					}
			},
			// 完成绑定
			complateAlipay(){
				this.alipayShow = false;
				this.getPayList(this.shopId);
			},
			// 解除绑定
			async delAlipayFn(){
				let params = { shopId: this.shopId,id:this.isAlipayId }
				uni.showLoading({
					title:'解除绑定中。。。'
				})
				const res = await this.$setHttp.delete('api/Shop/UnboundCollection', params,{header:{shopId:this.shopId}});
				uni.hideLoading()
				console.log(res)
				if(res.code=== 200){
					uni.showToast({
						title:'解除签约成功'
					});
					this.getPayList(this.shopId);
				}else{
					uni.showToast({
						title:'解除签约失败'
					})
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.alipay-pop{
		box-sizing: border-box;
		padding: 30rpx 30rpx 50rpx;
		width: 500rpx;
		.alipay-pop-text{
			font-weight: bold;
			text-align: center;
			margin-bottom: 20px;
		}
		.down-image{
			width: 30px;
			height: 30px;
			display: block;
			margin: 10px auto;
		}
	}
</style>
